package com.sltx.util;

import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

@Component
public class WordUtil {
    private Configuration configuration = null;

    /****
     * 模板文件存放的目录
     */
    @Value("${baseDir}")
    private  String  baseDir ;
    /***
     * 模板文件名称
     */
    private static final String  templateFile = "word.ftl";


    //文件保存路径
    @Value("${file-save-path}")
    private String fileSavePath;

    //项目启动根目录
    @Value("${server.servlet.context-path}")
    private String locationPath;

    public WordUtil(){
        configuration = new Configuration();
        configuration.setDefaultEncoding("UTF-8");
    }

    /**
     * 转换成word
     */
    public String createWord(PageData pd, HttpServletRequest request){
        Map<String,Object> dataMap=new HashMap<String,Object>();
        //构造参数
        getData(dataMap, pd);

        //后半段目录：
        String directory = "word/";

        //模板文件所在路径
        configuration.setClassForTemplateLoading(this.getClass(), templateFile);
        Template t=null;
        try {
            configuration.setDirectoryForTemplateLoading(new File(baseDir));
            t = configuration.getTemplate(templateFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //文件名称
        String fileName =  pd.get("entname")+  ".doc";
        //导出文件
        File outFile = new File(fileSavePath + directory, fileName);
        Writer out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }

        try {
            t.process(dataMap, out); //将填充数据填入模板文件并输出到目标文件

            //windows下
//            String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() +  locationPath + "/aaa/" + fileName;

            //linux下  request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + locationPath +
            String url =  request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + locationPath + fileSavePath + directory + fileName;
            System.out.println("文件上传，访问url  "  + url);
            return url;
        } catch (Exception e) {
            e.printStackTrace();
            return "文件生成错误";
        }

    }

    /****
     * 初始化数据map
     */
    private void getData(Map<String, Object> dataMap, Map pd) {

        dataMap.put("entname",pd.get("entname"));
        dataMap.put("man", pd.get("ent_fr"));
        dataMap.put("money", pd.get("ent_zczb"));
        dataMap.put("xydm", pd.get("uniscid"));
        dataMap.put("time", pd.get("ent_date"));
        dataMap.put("zcdz", pd.get("ent_dz"));
        dataMap.put("sjjjdz", pd.get("ent_retitle"));
        dataMap.put("jyfw", pd.get("ent_title"));
        dataMap.put("gjj", pd.get("ent_gjj_xl"));
        dataMap.put("qylb", pd.get("ent_zdqy_xl"));
        dataMap.put("wzqy", pd.get("ent_sfwz"));
        dataMap.put("yjxs", pd.get("ent_yjfs"));
        dataMap.put("xxtgdw", pd.get("ent_xxtgdw"));
        dataMap.put("yjqydw", pd.get("ent_yjqydw"));
        dataMap.put("qylxr", pd.get("ent_person"));
        dataMap.put("qylxdh", pd.get("ent_tel"));
        dataMap.put("jjgx", pd.get("gxd"));
        dataMap.put("tjsj", pd.get("submit_date"));
        dataMap.put("qyjj", pd.get("ent_jj"));

    }

}